package com.kotlin.myapplication.mananger

import java.util.Stack

import android.app.Activity
import android.content.Intent

import com.kotlin.myapplication.base.BaseActivity

/**
 * @author 作者： Administrator
 *
 * @version 创建时间：2014年8月26日下午6:19:04
 */
class AppManager private constructor() {

    private val activityStack = Stack<BaseActivity>()
    var isStartingActivity = false

    /**
     * 添加Activity到栈
     */
    fun addActivity(activity: BaseActivity) {
        activityStack.add(activity)
    }

    fun startActivity(intent: Intent, curActivity: Activity) {
        isStartingActivity = true
        curActivity.startActivity(intent)
    }

    /**
     * 获取当前Activity（栈顶Activity）
     */
    fun currentActivity(): BaseActivity? {
        return if (activityStack == null || activityStack.isEmpty()) {
            null
        } else activityStack.lastElement()
    }

    /**
     * 获取当前Activity（栈顶Activity） 没有找到则返回null
     */
    fun findActivity(cls: Class<*>): BaseActivity? {
        var activity: BaseActivity? = null
        for (aty in activityStack) {
            if (aty.javaClass == cls) {
                activity = aty
                break
            }
        }
        return activity
    }

    /**
     * 结束当前Activity（栈顶Activity）
     */
    fun finishActivity() {
        val activity = activityStack.lastElement()
        finishActivity(activity)
    }

    /**
     * 结束指定的Activity(重载)
     */
    fun finishActivity(activity: Activity?) {
        var activity = activity
        if (activity != null) {
            activityStack.remove(activity)
            activity.finish()
            activity = null
        }
    }

    /**
     * 结束指定的Activity(重载)
     */
    fun finishActivity(cls: Class<*>) {
        var delActivity: Activity? = null
        for (activity in activityStack) {
            if (activity.javaClass == cls) {
                delActivity = activity
                //finishActivity(activity);
            }
        }
        if (null != delActivity) {
            finishActivity(delActivity)
        }
    }

    /**
     * 关闭除了指定activity以外的全部activity 如果cls不存在于栈中，则栈全部清空
     *
     * @param cls
     */
    fun finishOthersActivity(cls: Class<*>) {
        val iterator = activityStack.iterator()
        while (iterator.hasNext()) {
            val activity = iterator.next()
            if (activity != null && activity.javaClass != cls) {
                iterator.remove()
                activity.finish()
            }
        }
    }

    /**
     * 结束所有Activity
     */
    fun finishAllActivity2() {
        var i = 0
        val size = activityStack.size
        while (i < size) {
            if (null != activityStack[i]) {
                activityStack[i].finish()
            }
            i++
        }
        activityStack.clear()
    }

    fun removeActivity(activity: BaseActivity) {
        activityStack.remove(activity)
    }

    companion object {

        /**
         * 单实例 , UI无需考虑多线程同步问题
         */
        val appManager = AppManager()

        fun finishAllActivity() {
            appManager.finishAllActivity2()
        }
    }


    //	/**
    //	 * 应用程序退出
    //	 */
    //	public void AppExit(Context context) {
    //		try {
    //			finishAllActivity();
    //			ActivityManager activityMgr = (ActivityManager) context
    //					.getSystemService(Context.ACTIVITY_SERVICE);
    //			activityMgr.killBackgroundProcesses(context.getPackageName());
    //			System.exit(0);
    //		} catch (Exception e) {
    //			System.exit(0);
    //		}
    //	}

}